package org.ipower.examsystem.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.ipower.examsystem.dao.CatalogDao;
import org.ipower.examsystem.dao.CooperationCatalogDao;
import org.ipower.examsystem.domain.Catalog;
import org.ipower.examsystem.domain.CooperationCatalog;
import org.ipower.platform.service.TreeCheckedData;
import org.ipower.platform.service.TreeCheckedDataCollection;
/**
 *  合作科目数据访问实现类。
 * @author jeasonyoung
 */
public class CooperationCatalogDaoImpl extends  ModuleGenericDaoImpl<CooperationCatalog> implements CooperationCatalogDao {
    private CatalogDao catalogDao = null;
    
    @Override
    public void setCatalogDao(CatalogDao dao) {
        this.catalogDao = dao;
    }

    @Override
    public boolean delete(String cooperationID) {
        if(cooperationID != null && !cooperationID.isEmpty()){
             this.delete("CooperationID", cooperationID);
            return true;
        }
        return false;
    }
  
    @Override
    public boolean update(String cooperationID, String[] catalogID) {
        if(cooperationID != null && !cooperationID.isEmpty()){
            this.delete(cooperationID);
            if(catalogID != null && catalogID.length > 0){
                for(String id : catalogID){
                    if(id != null && !id.isEmpty()){
                        CooperationCatalog data = new CooperationCatalog();
                        data.setCooperationID(cooperationID);
                        data.setCatalogID(id);
                        this.save(data);
                    }
                }
            }
            return true;
        }
        return false;
    }

    @Override
    public TreeCheckedDataCollection CreateCatalogTree(String cooperationID) {
        TreeCheckedDataCollection collection = new TreeCheckedDataCollection();
        if(this.catalogDao != null){
            List<Catalog> catalogs = this.catalogDao.loadAll();
            if(catalogs != null && catalogs.size() > 0){
                List<String>catalogIds = new ArrayList<String>();
                List<CooperationCatalog> list = this.loadAll("CooperationID", cooperationID);
                if(list != null && list.size() > 0){
                    for(int i = 0; i < list.size(); i++){
                        catalogIds.add(list.get(i).getCatalogID());
                    }
                }
                List<Catalog> removes = new ArrayList<Catalog>();
                for(Catalog c : catalogs){
                    if(c.getParentCatalogID() == null || c.getParentCatalogID().isEmpty()){
                         TreeCheckedData data = new TreeCheckedData();
                          data.setId(c.getCatalogID());
                          data.setText(c.getCatalogName());
                          data.setLeaf(true);
                          if(catalogIds != null && catalogIds.size() > 0){
                                data.setChecked(catalogIds.contains(data.getId()));
                         }
                         collection.add(data);
                         removes.add(c);
                    }
                }
                if(collection.size() > 0){
                    catalogs.removeAll(removes);
                    for(int i = 0; i < collection.size(); i++){
                        this.createTree(collection.get(i), catalogs, catalogIds);
                    }
                }
            }
        }
        return collection;
    }

    private void createTree(TreeCheckedData parent, List<Catalog> catalogs,List<String> catalogIds){
        if(parent != null){
            List<Catalog> childs = this.getChilds(catalogs,parent.getId());
            if(childs != null && childs.size() > 0){
                catalogs.removeAll(childs);
                for(int i = 0; i < childs.size(); i++){
                    Catalog c = childs.get(i);
                    TreeCheckedData data = new TreeCheckedData();
                    data.setId(c.getCatalogID());
                    data.setText(c.getCatalogName());
                    data.setLeaf(true);
                    if(catalogIds != null && catalogIds.size() > 0){
                        data.setChecked(catalogIds.contains(data.getId()));
                    }
                    this.createTree(data, catalogs, catalogIds);                    
                    parent.setLeaf(false);
                    parent.setExpanded(true);
                    parent.getChildren().add(data);
                }
            }
        }
    }

    private List<Catalog> getChilds(List<Catalog> catalogs,String parentId) {
        List<Catalog> result = new ArrayList<Catalog>();
        if(catalogs != null && catalogs.size() > 0){
            for(int i = 0; i < catalogs.size(); i++){
                Catalog c = catalogs.get(i);
                if(c.getParentCatalogID().equalsIgnoreCase(parentId)){
                    result.add(c);
                }
            }
        }
        return result;
    }
}